package main

import (
	"fmt"
	"strconv"
)

type ListNode struct {
	Val  int
	Next *ListNode
}

func InitListNode(a []int) *ListNode {
	length := len(a)
	if length == 0 {
		return nil
	}
	var pre, head *ListNode
	for i := 0; i < length; i++ {
		node := &ListNode{
			Val:  a[i],
			Next: nil,
		}
		if pre != nil {
			pre.Next = node
		}
		if head == nil {
			head = node
		}
		pre = node
	}
	return head
}

func Trans(head *ListNode) string {
	if head == nil {
		return ""
	}

	ret := ""
	current := head
	for current != nil {
		ret = fmt.Sprintf("%s%d", ret, current.Val)
		current = current.Next
	}
	return ret
}

func Reverse(head *ListNode) *ListNode {
	if head == nil {
		return nil
	}

	var pre, cur, next *ListNode = nil, head, nil
	for cur != nil {
		next = cur.Next
		cur.Next = pre
		pre = cur
		cur = next
	}
	return pre
}

func main() {
	listA := InitListNode([]int{1, 5, 16, 1})
	listB := InitListNode([]int{4, 5, 6})
	listA = Reverse(listA)
	listB = Reverse(listB)
	strA := Trans(listA)
	strB := Trans(listB)
	fmt.Println(strA)
	fmt.Println(strB)
	intA, _ := strconv.Atoi(strA)
	intB, _ := strconv.Atoi(strB)
	fmt.Println(intA + intB)
}
